package org.jeecg.modules.monitoring.j_monitoring.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import com.xkcoding.http.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.monitoring.j_monitoring.entity.GatewayWarningCount;
import org.jeecg.modules.monitoring.j_monitoring.entity.GatewayWarningHistory;
import org.jeecg.modules.monitoring.j_monitoring.service.IGatewaySystemFdnService;
import org.jeecg.modules.monitoring.j_monitoring.service.IGatewayWarningHistoryService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 网关告警历史数据
 * @Author: xiaoyq
 * @Date: 2023-05-09
 * @Version: V1.0
 */
@Api(tags = "网关告警历史数据")
@RestController
@RequestMapping("/gatewayWarningHistory/gatewayWarningHistory")
@Slf4j
public class GatewayWarningHistoryController extends JeecgController<GatewayWarningHistory, IGatewayWarningHistoryService> {
    @Autowired
    private IGatewaySystemFdnService fdnService;

    /**
     * 分页列表查询
     *
     * @param gatewayWarningHistory
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @RequiresPermissions("网关告警:查询")
    @ApiOperation(value = "网关告警历史数据-分页列表查询", notes = "网关告警历史数据-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<GatewayWarningHistory>> queryPageList(GatewayWarningHistory gatewayWarningHistory,
                                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                              @RequestParam(name = "type") Integer type,
                                                              @RequestParam(name = "dataId") String id,
								   HttpServletRequest req) {

        QueryWrapper<GatewayWarningHistory> queryWrapper = QueryGenerator.initQueryWrapper(gatewayWarningHistory, req.getParameterMap());
        Page<GatewayWarningHistory> page = new Page<GatewayWarningHistory>(pageNo, pageSize);

        if (StringUtil.isEmpty(id)){
            return Result.ok(page);
        }
        String fdn = fdnService.gateFdnByTypeAndId(type, id);
        if (StringUtil.isEmpty(fdn)){
            return Result.ok(page);
        }


        queryWrapper.like("fdn",fdn);

        IPage<GatewayWarningHistory> pageList = service.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param gatewayWarningHistory
     * @return
     */
    @AutoLog(value = "网关告警历史数据-添加")
    @ApiOperation(value = "网关告警历史数据-添加", notes = "网关告警历史数据-添加")
    @RequiresPermissions("网关告警:添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody GatewayWarningHistory gatewayWarningHistory) {
        service.save(gatewayWarningHistory);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param gatewayWarningHistory
     * @return
     */
    @AutoLog(value = "网关告警历史数据-编辑")
    @ApiOperation(value = "网关告警历史数据-编辑", notes = "网关告警历史数据-编辑")
    @RequiresPermissions("网关告警:编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody GatewayWarningHistory gatewayWarningHistory) {
        service.updateById(gatewayWarningHistory);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "网关告警历史数据-通过id删除")
    @ApiOperation(value = "网关告警历史数据-通过id删除", notes = "网关告警历史数据-通过id删除")
    @RequiresPermissions("网关告警:删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        service.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "网关告警历史数据-批量删除")
    @ApiOperation(value = "网关告警历史数据-批量删除", notes = "网关告警历史数据-批量删除")
    @RequiresPermissions("网关告警:批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.service.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "网关告警历史数据-通过id查询")
    @ApiOperation(value = "网关告警历史数据-通过id查询", notes = "网关告警历史数据-通过id查询")
    @GetMapping(value = "/queryById")
    @RequiresPermissions("网关告警:查询")
    public Result<GatewayWarningHistory> queryById(@RequestParam(name = "id", required = true) String id) {
        GatewayWarningHistory gatewayWarningHistory = service.getById(id);
        if (gatewayWarningHistory == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(gatewayWarningHistory);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param gatewayWarningHistory
     */
    @RequiresPermissions("网关告警:导出")
    @RequestMapping(value = "/exportXls", method = RequestMethod.GET)
    @ApiOperation(value="网关告警历史数据-导出", notes="网关告警-导出")
    public ModelAndView exportXls(HttpServletRequest request, GatewayWarningHistory gatewayWarningHistory) {
        return super.exportXls(request, gatewayWarningHistory, GatewayWarningHistory.class, "网关告警历史数据");
    }


    @RequiresPermissions("网关告警:查询")
    @ApiOperation(value = "网关告警历史数据-分页列表查询", notes = "网关告警历史数据-分页列表查询")
    @GetMapping(value = "/getWarningCount")
    public Result<GatewayWarningCount> getWarningCount(
            @RequestParam(name = "type") Integer type,
            @RequestParam(name = "dataId") String id) {
        String fdn = fdnService.gateFdnByTypeAndId(type, id);
        if (StringUtil.isEmpty(fdn)) {
            return Result.error("未找到设备fdn参数");
        }
        return Result.OK(service.getWarningCount(fdn));
    }

}
